var express = require('express');
var router = express.Router();
const studentService = require('../service/studentService');

router.post('/',async function(req,res){
    let {name,gender,age,headImg,classes} = req.body;
     let data = await studentService.insert({name,gender,age,headImg,classes});
     res.send(data);
});

router.get('/',async function(req,res){
    let {type,value,current,pageSize} = req.query;
    let searchParams = {};
    searchParams[type] = value;
    let data = await studentService.find({current:current || 1,pageSize:pageSize || 5},searchParams);
    res.send(data);
});


router.get('/:id',async function(req,res){
    let {id} = req.params;
    let data = await studentService.findById(id);
    res.send(data);
});


router.delete('/:id',async function(req,res){
    let {id} = req.params;
    let data = await studentService.delete(id);
    res.send(data);
});

router.put('/:id',async function(req,res){
    let {id} = req.params;
    let {name,gender,age} = req.body;
    console.log({_id:id,name,gender,age})
    let data = await studentService.update({_id:id,name,gender,age});
    res.send(data);
});

module.exports = router;